﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QLab.Model
{
    public class CommandIterator
    {
        private ObservableList<string> _commands;
        private int _currentPosition = 0;

        /// <summary>
        /// Command iterator can only be created from command history
        /// </summary>
        /// <param name="Commands">List of commands</param>

        internal CommandIterator(ObservableList<string> Commands)
        {
            _commands = Commands;
            _commands.ItemAdded += new ObservableList<string>.ItemAddedEventHandler(CommandAdded);
            _commands.ItemRemoved += new ObservableList<string>.ItemRemovedEventHandler(CommandRemoved);
            _commands.ListCleared += new ObservableList<string>.ListClearedEventHandler(ListCleared);
        }

        public bool IsAtBegin()
        {
            return _currentPosition == -1;
        }

        public bool IsAtEnd()
        {
            return _currentPosition == _commands.Count;
        }
        /// <summary>
        /// Next command in command history
        /// </summary>
        /// <returns>Next command</returns>
        public string Next()
        {
            _currentPosition++;

            if (_currentPosition + 1 > _commands.Count)
            {
                _currentPosition = _commands.Count;
                return null;
            }

            return _commands[_currentPosition];
        }

        /// <summary>
        /// Previous command in command history
        /// </summary>
        /// <returns>previous command</returns>
        public string Previous()
        {
            _currentPosition--;

            if (_currentPosition < 0)
            {
                _currentPosition = -1;
                return null;
            }
            return _commands[_currentPosition];
        }

        /// <summary>
        /// Monitors when new command has been added to command history and updates current position to be after last
        /// </summary>
        private void CommandAdded(object source, ObservableList<string>.ListChangedEventArgs e)
        {
            _currentPosition = e.index + 1;		//Pri dodavanju komande hocemo da ona bude prva sledeca kad pritisnemo taster na gore
        }

        /// <summary>
        /// When command history has been cleared, current position is set to 0
        /// </summary>
        private void ListCleared(object source, EventArgs e)
        {
            _currentPosition = 0;
        }

        /// <summary>
        /// Monitors when commands are removed from command history and updates current position
        /// </summary>
        private void CommandRemoved(object source, ObservableList<string>.ListChangedEventArgs e)
        {
            //Ako se komanda nalazi ispod trenutne pozicije, moramo da azuriramo trenutnu poziciju da ne bi smo ispali iz opsega komandi
            if (e.index < _currentPosition)
                _currentPosition--;
        }
        public void ToLastCommand()
        {
            _currentPosition = _commands.Count;
        }
    }
}

